/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.beActionAssemblerGen;

import com.inspur.edp.bef.api.action.assembler.IBEActionAssembler;
import com.inspur.edp.bef.api.attr.AssemblerMethodAttribute;
import com.inspur.edp.bef.api.be.IBEContext;
import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.component.ICompParameter;
import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.detailcmpentity.be.BEMethod;
import com.inspur.edp.bef.component.detailcmpentity.be.BEParamCollection;
import com.inspur.edp.bef.dtgenerator.BEGeneratorContext;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.InterfaceMethodGenerateInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.InterfaceMethodGenerator;
import com.inspur.edp.caf.generator.method.InterfaceMethodGeneratorContext;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.StringLiteral;

// 自定义mgraction的操作权限如何控制?
public class CustomBEActionMethodGenerator extends InterfaceMethodGenerator {

  // region Static Members
  public static final String methodNameTemplate = "Get%1$sActionAssembler";
  public static final String beContextParName = "beContext";

  // region Basic Info
  private BEGeneratorContext beGenCtx;
  private BizAction action;
  private BEMethod method;

  public CustomBEActionMethodGenerator(BEGeneratorContext beGenCtx, BizAction action) {
    this.beGenCtx = beGenCtx;
    this.action = action;
  }

  public static String getMethodName(BizAction mgrAction) {
    return String.format(methodNameTemplate, mgrAction.getCode());
  }

  public static java.util.ArrayList<ParameterInfo> getMethodParameters(
      BEMethod method, BEGeneratorContext beGenCtx) {
    ParameterInfo tempVar = new ParameterInfo();
    tempVar.setParamName(beContextParName);
    tempVar.setParamType(
        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEContext.class));
    java.util.ArrayList<ParameterInfo> rez =
        new java.util.ArrayList<ParameterInfo>(
            java.util.Arrays.asList(tempVar));
    if (method.getParameterCollection() != null) {
      BEParamCollection params = method.getBeParams();
      for (Object item : params) {
        ICompParameter parameter = (ICompParameter) item;
        rez.add(com.inspur.edp.cef.generator.overall.Utils.buildParGenInfo(parameter, beGenCtx, null));
      }
    }
    return rez;
  }

  @Override
  protected InterfaceMethodGeneratorContext createClassInfoContext() {
    return new InterfaceMethodGeneratorContext();
  }

  @Override
  protected InterfaceMethodGenerateInfo createClassMethodInfo() {
    return new InterfaceMethodGenerateInfo();
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>();
  }

  @Override
  protected String getMethodName() {
    return getMethodName(action);
  }

  @Override
  protected void doInitialize() {
    GspMetadata metadata =
        beGenCtx
            .getMetadataPool()
            .getMetadata(action.getComponentPkgName(), action.getComponentId());
    //		if (metadata.getHeader().getType().equals("Component"))
    //		{
    //			GspComponentMetadata gspComp = (GspComponentMetadata)((metadata.Content instanceof
    // GspComponentMetadata) ? metadata.Content : null);
    //			var compMetadata = gspComp != null ? (BEComponent)gspComp.Content : null;
    //			if (compMetadata == null)
    //			{
    //				throw new GSPException(ErrorCode.BeCompNotFound, Utils.FormatExceptionMsg("BE动作构件不存在: " +
    // action.getComponentId()));
    //			}
    //			method = compMetadata.BEMethod;
    //		}
    //		else
    //		{
    GspComponent comp = (GspComponent) metadata.getContent();
    if (!comp.getComponentType().equals("BEComponent")) {
      throw new RuntimeException("错误的类型：" + comp.getComponentType());
    }
    Object tempVar = comp.getMethod();
    method = (BEMethod) tempVar;
    //		}
  }

  // endregion

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return getMethodParameters(method, beGenCtx);
  }

  @Override
  protected TypeInfo getReturnType() {
    return com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEActionAssembler.class);
  }

  @Override
  protected java.util.ArrayList<AnnotationInfo> getAttributeList() {
    java.util.ArrayList<AnnotationInfo> tempVar = new java.util.ArrayList<AnnotationInfo>(1);
    AnnotationInfo info = new AnnotationInfo();
    info.setTypeInfo(
        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(AssemblerMethodAttribute.class));
    info.setAnnotationType(AnnotationType.SingleMember);
    StringLiteral valueLiteral = ast.newStringLiteral();
    valueLiteral.setLiteralValue(getAssemblerMethodName());
    info.setSingleParam(valueLiteral);
    tempVar.add(info);
    return tempVar;
  }

  private String getAssemblerMethodName() {
    // TODO: 应该是: beCode + "层次" + 动作编号; "层次"暂时先不加;
    return beGenCtx.getBE().getCode() + "" + action.getCode();
  }

  // endregion
}
